﻿package net.wangit.view.unit.util;

import java.util.HashMap;
import java.util.Map;

import net.wangit.LabelUtil;
import net.wangit.MapList;
import net.wangit.context.ActionContext;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.component.RadioElement;
import net.wangit.view.enumeration.EnumerationUtil;


public class QueryElementUtil {

	public QueryElementUtil() {
	}

	public static void addBlank(ElementComponent element) {
		MapList data = getData(element);
		if (data != null)
			data.insertRow(0, EnumerationUtil.getBlankRow());
	}

	public static void addAll(ActionContext ac, ElementComponent element) {
		MapList data = getData(element);
		if (data != null) {
			Map allRow = new HashMap();
			allRow.put("value", "");
			allRow.put("name", LabelUtil.get(ac, "components", "element.52.2"));
			data.insertRow(0, allRow);
		}
	}

	public static void setCheckBoxFieldRadio(ActionContext ac,
			ElementComponent ec) {
		RadioElement radio = new RadioElement();
		radio.setId(ec.getId());
		radio.setParent(ec.getParent());
		radio.setShowMode(ec.getShowMode());
		radio.setDefaultValue(ec.getDefaultValue());
		MapList list = new MapList();
		if (ec.isRequired()) {
			list.put(0, "value", "1");
			list
					.put(0, "name", LabelUtil.get(ac, "components",
							"element.52.1"));
			list.put(1, "value", "0");
			list
					.put(1, "name", LabelUtil.get(ac, "components",
							"element.52.0"));
		} else {
			list.put(0, "value", "");
			list
					.put(0, "name", LabelUtil.get(ac, "components",
							"element.52.2"));
			list.put(1, "value", "1");
			list
					.put(1, "name", LabelUtil.get(ac, "components",
							"element.52.1"));
			list.put(2, "value", "0");
			list
					.put(2, "name", LabelUtil.get(ac, "components",
							"element.52.0"));
		}
		radio.setObject(list);
		ec.setObject(radio);
	}

	private static MapList getData(ElementComponent element) {
		if (!element.isRequired()) {
			MapList data = (MapList) element.getObject();
			if (data != null && data.findRowIndex("value", "") < 0)
				return data;
		}
		return null;
	}
}
